// pages/line/list/list.ts
import { doList, doDel } from '~/api/line'
let isRefresh = false
Page({

  /**
   * 页面的初始数据
   */
  data: {
    info: {
      id: 0,
      name: '',
      mobile: '',
      province: '',
      serviceSite: '',
      userName: ''
    } as any,
    showConfirm: false,
    status: 'loading' as 'loading' | 'loadmore' | 'nomore',
    tableData: [] as any[],
    oldId: 0
  },
  handDel(e: any) {
    const dataset = e.currentTarget.dataset
    this.setData({
      showConfirm: true,
      oldId: dataset.id
    })
  },
  handEdit(e: any) {
    const { tableData } = this.data
    const dataset = e.currentTarget.dataset
    const info = tableData.find(el => el.id == dataset.id)
    wx.navigateTo({
      url: `/pages/line/add/add?&info=${encodeURIComponent(JSON.stringify(info))}`,
      events: {
        success: () => {
          this.fetchData()
        }
      }
    })
  },
  async handConfirm() {
    this.closeDialog()
    const { oldId } = this.data
    try {
      await doDel({
        id: oldId
      })
      wx.showToast({
        title: '删除成功',
        icon: 'none'
      })
      this.fetchData()
    } catch (error) {
      wx.showToast({
        title: '删除失败',
        icon: 'none'
      })
    }
  },
  closeDialog(this: any) {
    this.setData({ showConfirm: false });
  },
  handAdd() {
    wx.navigateTo({
      url: '/pages/line/add/add',
      events: {
        success: () => {
          this.fetchData()
        }
      }
    })
  },
  async fetchData() {
    this.setData({
      status: 'loading'
    })
    const data = await doList({})
    const arr = [] as any[]
    data.roadLine.forEach(el => {
      const index = arr.findIndex(item => item.star == el.destProvince && item.end == el.destCity)
      if (index >= 0) {
        arr[index].childer.push({
          id: el.id,
          name: el.destArea
        })
      } else {
        if (el.destArea) {
          arr.push({
            id: el.id,
            star: el.destProvince,
            end: el.destCity,
            childer: [{
              id: el.id,
              name: el.destArea
            }]
          })
        } else {
          arr.push({
            id: el.id,
            star: el.destProvince,
            end: el.destCity,
            childer: [{
              id: el.id,
              name: '全境'
            }]
          })
        }
      }
    })
    console.log(arr)
    this.setData({
      info: data,
      tableData: arr,
      status: 'nomore'
    })
    if (isRefresh) {
      wx.stopPullDownRefresh();
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    this.fetchData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    isRefresh = true
    this.fetchData()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})